<template>
  <div class="app-container">
    <!-- 全局导航栏 -->
    <header class="global-header">
      <div class="header-content">
        <div class="logo-section">
          <h1 class="app-title">
            <router-link to="/">OpenNav</router-link>
          </h1>
        </div>
        
        <div class="user-section" v-if="userStore.isLoggedIn">
          <div class="theme-toggle">
            <span>夜间模式</span>
            <el-switch 
              :model-value="themeStore.isDark" 
              active-text="" 
              inactive-text="" 
              @change="themeStore.toggleTheme"
            />
          </div>
          <el-dropdown trigger="click" @command="handleCommand">
            <div class="user-info-trigger">
              <el-avatar :size="36" :src="userStore.avatar">
                {{ userStore.avatarOrInitial }}
              </el-avatar>
              <span class="user-name">{{ userStore.displayName }}</span>
            </div>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="userInfo">个人信息</el-dropdown-item>
                <el-dropdown-item command="logout" :loading="userStore.loading">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
        
        <div class="auth-section" v-else>
          <div class="theme-toggle">
            <span>夜间模式</span>
            <el-switch 
              :model-value="themeStore.isDark" 
              active-text="" 
              inactive-text="" 
              @change="themeStore.toggleTheme"
            />
          </div>
          <router-link to="/login" class="login-link">登录</router-link>
          <router-link to="/register" class="register-link">注册</router-link>
        </div>
      </div>
    </header>
    
    <!-- 主要内容区域 -->
    <main class="main-content">
      <router-view v-slot="{ Component }">
        <transition name="fade" mode="out-in">
          <component :is="Component" />
        </transition>
      </router-view>
    </main>
  </div>
</template>

<script setup lang="ts">
import { onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElDropdown, ElDropdownMenu, ElDropdownItem, ElSwitch } from 'element-plus'
import { useUserStore } from './store/modules/user'
import { useThemeStore } from './store/modules/theme'

const router = useRouter()
const userStore = useUserStore()
const themeStore = useThemeStore()

// 初始化主题
onMounted(() => {
  themeStore.loadTheme()
})

// 处理命令
const handleCommand = (command: string) => {
  switch (command) {
    case 'userInfo':
      goToUserInfo()
      break
    case 'logout':
      handleLogout()
      break
  }
}

// 处理退出登录
const handleLogout = async () => {
  await userStore.logout()
  // 跳转到登录页
  router.replace('/login')
}

// 跳转到用户信息页面
const goToUserInfo = () => {
  router.push('/user/info')
}

// 初始化
onMounted(async () => {
  if (userStore.isLoggedIn) {
    try {
      await userStore.getUserInfo()
    } catch (error) {
      console.error('获取用户信息失败', error)
    }
  }
})

// 监听路由变化，在需要登录的页面重新获取用户信息
router.afterEach(async (to) => {
  if (userStore.isLoggedIn && to.meta.requiresAuth) {
    try {
      await userStore.getUserInfo()
    } catch (error) {
      console.error('获取用户信息失败', error)
      router.replace('/login')
    }
  }
})
</script>

<style>
/* 全局重置样式 */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html, body {
  width: 100%;
  height: 100%;
  overflow: hidden;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'Helvetica Neue', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  color: var(--text-primary);
  background-color: var(--background-color);
}

#app {
  width: 100%;
  height: 100%;
}

.app-container {
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100%;
  overflow: hidden;
}

/* 全局导航栏样式 */
.global-header {
  background-color: var(--background-color-header);
  box-shadow: var(--box-shadow-base);
  position: relative;
  z-index: 1000;
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 24px;
  height: 64px;
  max-width: 1440px;
  margin: 0 auto;
}

.logo-section .app-title {
  font-size: 24px;
  font-weight: 600;
  margin: 0;
}

.logo-section .app-title a {
  color: var(--primary-color);
  text-decoration: none;
  transition: color 0.3s;
}

.logo-section .app-title a:hover {
  color: var(--primary-hover);
}

/* 用户区域样式 */
.user-section {
  display: flex;
  align-items: center;
}

.user-info-trigger {
  display: flex;
  align-items: center;
  cursor: pointer;
  padding: 8px 12px;
  border-radius: var(--border-radius-base);
  transition: background-color 0.3s;
}

.user-info-trigger:hover {
  background-color: var(--background-color-hover);
}

.user-name {
  margin-left: 8px;
  font-size: 14px;
  color: var(--text-primary);
}

/* 认证区域样式 */
.auth-section {
  display: flex;
  gap: 16px;
}

.login-link, .register-link {
  padding: 8px 16px;
  border-radius: 4px;
  font-size: 14px;
  transition: all 0.3s;
}

.login-link {
  color: var(--primary-color);
}

.login-link:hover {
  background-color: rgba(64, 158, 255, 0.1);
}

.register-link {
  background-color: var(--primary-color);
  color: white;
}

.register-link:hover {
  background-color: var(--primary-hover);
  color: white;
}

/* 主要内容区域 */
.main-content {
  flex: 1;
  overflow: auto;
  padding: 24px;
  min-height: 0;
}

/* 过渡动画 */
.fade-enter-active, .fade-leave-active {
  transition: opacity 0.3s;
}

.fade-enter-from, .fade-leave-to {
  opacity: 0;
}

/* Element Plus 样式覆盖 */
.el-input__inner {
  border-radius: 8px;
}

.el-tag {
  transition: all 0.3s ease;
}

.el-empty {
  color: var(--text-secondary);
}

/* 链接样式 */
a {
  color: #409eff;
  text-decoration: none;
  transition: color 0.3s;
}

a:hover {
  color: #66b1ff;
}

/* 按钮全局样式 */
button {
  font-family: inherit;
}

/* 工具类 */
.text-center {
  text-align: center;
}

.text-left {
  text-align: left;
}

.text-right {
  text-align: right;
}

/* 主题切换开关样式 */
.theme-toggle {
  margin-right: 20px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.theme-toggle .el-switch__label {
  color: var(--text-regular);
}

/* Element Plus 暗色主题适配 */
.dark-theme .el-input__inner,
.dark-theme .el-select__wrapper,
.dark-theme .el-textarea__inner {
  background-color: var(--background-color-card);
  color: var(--text-primary);
  border-color: var(--border-base);
}

.dark-theme .el-card,
.dark-theme .el-dialog__wrapper .el-dialog,
.dark-theme .el-dropdown-menu {
  background-color: var(--background-color-card);
  border-color: var(--border-base);
}

.dark-theme .el-dropdown-menu__item {
  color: var(--text-primary);
}

.dark-theme .el-dropdown-menu__item:hover {
  background-color: var(--background-color-hover);
}

.dark-theme .el-table {
  background-color: var(--background-color-card);
}

.dark-theme .el-table th,
.dark-theme .el-table td {
  background-color: var(--background-color-card);
  color: var(--text-primary);
  border-color: var(--border-light);
}

/* 滚动条全局样式 */
::-webkit-scrollbar {
  width: 8px;
  height: 8px;
}

::-webkit-scrollbar-track {
  background: #f1f1f1;
  border-radius: 4px;
}

::-webkit-scrollbar-thumb {
  background: #c1c1c1;
  border-radius: 4px;
  transition: background 0.3s;
}

::-webkit-scrollbar-thumb:hover {
  background: #a8a8a8;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .header-content {
    padding: 0 16px;
  }
  
  .logo-section .app-title {
    font-size: 20px;
  }
  
  .user-name {
    display: none;
  }
  
  .main-content {
    padding: 16px;
  }
}
</style>
